//
//  SelectViewController.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "SelectViewController.h"
#import "SelectView.h"
#import "Resources.h"
#import "Lunbo.h"
#import "GSYTabbarViewController.h"
#import "MJRefresh.h"

@interface SelectViewController ()<CJSSelectViewDelegate>

@property (nonatomic, retain) SelectView *selectView;

@property (nonatomic, retain) NSMutableArray *selectTenArray;

@end

@implementation SelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.selectView = [[SelectView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    self.selectView.backgroundColor = self.view.backgroundColor;
    self.selectView.delegate = self;
    [self.view addSubview:self.selectView];
    
    [self AFNWorking];
}

- (void)AFNWorking {
    
    NSString *strUrl = @"http://api.breadtrip.com/v2/index/?lat=38.88254539124397&lng=121.539674658471";
    
    [Resources getDataWithName:strUrl block:^(id result) {
        
        NSDictionary *bigDic = result;
        NSArray *bigArray = [[bigDic objectForKey:@"data"] objectForKey:@"elements"];
        NSArray *array = nil;
        self.selectTenArray = [NSMutableArray array];
        self.fourArray = [NSMutableArray array];
        for (NSDictionary *dic in bigArray) {
            
            NSString *type = [dic objectForKey:@"type"];
            if ([type integerValue] == 1) {
                
                array = [[dic objectForKey:@"data"] firstObject];
            }
            if ([type integerValue] == 10) {
                
                NSDictionary *tDic = [[dic objectForKey:@"data"] firstObject];
                TypeTen *typeTen = [[TypeTen alloc] init];
                [typeTen setValuesForKeysWithDictionary:tDic];
                [self.selectTenArray addObject:typeTen];
            }
            
            if ([type integerValue] == 4) {
                
                NSDictionary *tDic = [[dic objectForKey:@"data"] firstObject];
                TypeFour *typeFour = [[TypeFour alloc] init];
                [typeFour setValuesForKeysWithDictionary:tDic];
                [self.fourArray addObject:typeFour];
            }
        }
        self.selectView.fourArray = self.fourArray;
        self.selectView.tenArray = self.selectTenArray;
        
        NSMutableArray *lunboArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            
            Lunbo *lunbo = [[Lunbo alloc] init];
            [lunbo setValuesForKeysWithDictionary:dic];
            [lunboArray addObject:lunbo];
        }
        self.selectView.lunboArray = lunboArray;
        [self.selectView endRefresh];
        
    } error:^{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self.selectView endRefresh];
        }];
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }];
}

- (void)AFNWorkingWithNext:(NSString *)next_start {
    
    NSString *strUrl = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/index/?lat=38.88251387874955&lng=121.5397645821401&next_start=%@", next_start];
    
    [Resources getDataWithName:strUrl block:^(id result) {
        
        NSDictionary *bigDic = result;
        NSArray *bigArray = [[bigDic objectForKey:@"data"] objectForKey:@"elements"];
        
        for (NSDictionary *dic in bigArray) {
            
            NSDictionary *fDic = [[dic objectForKey:@"data"] firstObject];
            TypeFour *typeFour = [[TypeFour alloc] init];
            [typeFour setValuesForKeysWithDictionary:fDic];
            [self.fourArray addObject:typeFour];
        }
        
        self.selectView.fourArray = self.fourArray;
        [self.selectView endFooterRefresh];
        
    } error:^{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self.selectView endRefresh];
        }];
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }];
}

- (void)getDataWithNext:(BOOL)isNext {
    
    if (isNext) {
        [self AFNWorkingWithNext:[[self.fourArray lastObject] tId]];
    }
    else {
        [self AFNWorking];
    }
}

#pragma mark - push详情页面
- (void)pushDetailViewController:detailViewController {
    
    GSYTabbarViewController *baseTabbarC = (GSYTabbarViewController *)self.tabBarController;
    [baseTabbarC isHiddenTabBar:YES withViewController:detailViewController];
    [self.navigationController pushViewController:detailViewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
